package com.shanxi.cz.module.biz.framework.nh.config;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shanxi.cz.framework.common.util.http.HttpUtils;
import com.shanxi.cz.module.biz.controller.admin.ammeterbasicinfo.vo.AmmeterBasicInfoSaveReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 集社云能耗 API
 */
@Slf4j
@Configuration
//@EnableConfigurationProperties(NhProperties.class)
public class NhApi {

//    @Resource
//    private NhProperties nhProperties;

    //host: http://nhapi.yunjichaobiao.com/WebApi
    //    api_key: 21D61524E62F445B8A01F7AFB63ECB38
//    @Value("${cz.nhapi.host}")
    private static String host = "nhapi.yunjichaobiao.com/WebApi";

//    @Value("${cz.nhapi.api_key}")
    private static String apiKey = "21D61524E62F445B8A01F7AFB63ECB38";

    /************ 以下是电表接口逻辑 ************/

    /**
     * 查询电表档案信息
     *
     * @param meterAddress 电表地址，多个用英文逗号隔开,示例:65879521,89751621,查询所有不传值
     * @return 电表档案信息
     */
    public List<AmmeterBasicInfoSaveReqVO> getAmmeterBasicInfo(String meterAddress) {
        Map<String, String> headers = generateHeaders();
        TreeMap<String, Object> body = new TreeMap<>();
        body.put("meterAddress", meterAddress);
        String responseBody = HttpUtils.post(host + "/NH2/AmmeterBasicInfo", headers, JSONUtil.toJsonStr(body));
        JSONObject entries = JSONUtil.parseObj(responseBody);
        if (entries.getBool("IsSuccess")) {
            JSONArray data = entries.getJSONArray("Data");
            Gson gson = new Gson();
            Type listType = new TypeToken<List<AmmeterBasicInfoSaveReqVO>>(){}.getType();
            List<AmmeterBasicInfoSaveReqVO> ammeterBasicInfoList = gson.fromJson(data.toString(), listType);
//            List<AmmeterBasicInfoSaveReqVO> ammeterBasicInfoList = JSONUtil.toList(data, AmmeterBasicInfoSaveReqVO.class);
            return ammeterBasicInfoList;


        } else {
            return null;
        }
    }

    /**
     * 查询电量
     */
    public String getElectricQuantity(String meterAddress) {
        Map<String, String> headers = generateHeaders();
        TreeMap<String, Object> body = new TreeMap<>();
        body.put("meterAddress", meterAddress);
        String responseBody = HttpUtils.post(host + "/NH2/ElectricQuantity", headers, JSONUtil.toJsonStr(body));
        JSONObject entries = JSONUtil.parseObj(responseBody);
        if (entries.getBool("IsSuccess")) {
            JSONArray data = entries.getJSONArray("Data");
            return null;
        } else {
            return null;
        }
    }

    /**
     * 查询断电信息
     */
    public String getAmmeterStateList(String meterAddress) {
        Map<String, String> headers = generateHeaders();
        TreeMap<String, Object> body = new TreeMap<>();
        body.put("meterAddress", meterAddress);
        String responseBody = HttpUtils.post(host + "/NH2/AmmeterStateList", headers, JSONUtil.toJsonStr(body));
        JSONObject entries = JSONUtil.parseObj(responseBody);
        if (entries.getBool("IsSuccess")) {
            JSONArray data = entries.getJSONArray("Data");
            return null;
        } else {
            return null;
        }
    }

    /**
     * 查询功率
     */
    public String getElectricPower(String meterAddress) {
        Map<String, String> headers = generateHeaders();
        TreeMap<String, Object> body = new TreeMap<>();
        body.put("meterAddress", meterAddress);
        String responseBody = HttpUtils.post(host + "/NH2/ElectricPower", headers, JSONUtil.toJsonStr(body));
        JSONObject entries = JSONUtil.parseObj(responseBody);
        if (entries.getBool("IsSuccess")) {
            JSONArray data = entries.getJSONArray("Data");
            return null;
        } else {
            return null;
        }
    }

    /**
     * 查询电压电流
     */
    public String getVoltageCurrent(String meterAddress) {
        Map<String, String> headers = generateHeaders();
        TreeMap<String, Object> body = new TreeMap<>();
        body.put("meterAddress", meterAddress);
        String responseBody = HttpUtils.post(host + "/NH2/VoltageCurrent", headers, JSONUtil.toJsonStr(body));
        JSONObject entries = JSONUtil.parseObj(responseBody);
        if (entries.getBool("IsSuccess")) {
            JSONArray data = entries.getJSONArray("Data");
            return null;
        } else {
            return null;
        }
    }

    /**
     * 查询相位角电网频率
     */
    public String getPhaseAngle(String meterAddress) {
        Map<String, String> headers = generateHeaders();
        TreeMap<String, Object> body = new TreeMap<>();
        body.put("meterAddress", meterAddress);
        String responseBody = HttpUtils.post(host + "/NH2/PhaseAngle", headers, JSONUtil.toJsonStr(body));
        JSONObject entries = JSONUtil.parseObj(responseBody);
        if (entries.getBool("IsSuccess")) {
            JSONArray data = entries.getJSONArray("Data");
            return null;
        } else {
            return null;
        }
    }


    private Map<String, String> generateHeaders() {
        Map<String, String> headers = new HashMap<>();
        headers.put("API_KEY", apiKey);
        return headers;
    }

}
